package com.ada.transfer.service.impl;

import com.ada.transfer.anno.Autowired;
import com.ada.transfer.anno.Service;
import com.ada.transfer.anno.Transactional;
import com.ada.transfer.dao.AccountDao;
import com.ada.transfer.dao.impl.JdbcAccountDaoImpl;
import com.ada.transfer.factory.BeanFactory;
import com.ada.transfer.pojo.Account;
import com.ada.transfer.service.TransferService;
import com.ada.transfer.utils.ConnectionUtils;
import com.ada.transfer.utils.TransactionManager;
@Transactional
@Service
public class TransferServiceImpl implements TransferService {
    //最佳状态
    @Autowired
    private AccountDao accountDao;

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        //有了代理对象就可以去掉非业务的东西了
        //try {
        //    //开启事务
        //    TransactionManager.getInstance().beginTransaction();
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            //int i=1/0;
            accountDao.updateAccountByCardNo(to);
            accountDao.updateAccountByCardNo(from);

        //    //提交事务
        //    TransactionManager.getInstance().commit();
        //}catch (Exception e){
        //    //回滚事务
        //    TransactionManager.getInstance().rollback();
        //    //让上层捕获
        //    throw e;
        //}

    }
}
